package com.zensun.common.core.page;

import cn.hutool.core.util.StrUtil;
import com.zensun.common.utils.spring.ServletUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 分页数据
 *
 * @author gmk
 */
@ApiModel("分页实体类")
@Data
public class PageDomain implements Serializable {
    /**
     * 当前记录起始索引
     */
    @ApiModelProperty("当前记录起始索引")
    private Integer pageNum;

    /**
     * 每页显示记录数
     */
    @ApiModelProperty("每页显示记录数")
    private Integer pageSize;

    /**
     * 排序列
     */
    @ApiModelProperty("排序列")
    private String orderByColumn;

    /**
     * 排序的方向desc或者asc
     */
    @ApiModelProperty("排序方式")
    private Boolean isAsc;

    public PageDomain() {
        this.pageNum = ServletUtils.getParameterToInt("pageNum");
        this.pageSize = ServletUtils.getParameterToInt("pageSize");
        this.orderByColumn = ServletUtils.getParameter("orderByColumn");
        this.isAsc = Boolean.valueOf(ServletUtils.getParameter("isAsc"));
    }

    @ApiModelProperty(value = "orderby", hidden = true)
    public String getOrderBy() {
        if (StrUtil.isBlank(orderByColumn)) {
            return "";
        }
        String paixu = isAsc ? " asc" : " desc";
        return StrUtil.toUnderlineCase(orderByColumn) + paixu;
    }
}
